<?php include_once TMPL_DIR.'layout/header.php';?>
<?php
$path	=	$_GET['path'];
$arr	=	explode('/', $path);

//include_once 'models/base.php';
$load->model('userModel');
include_once 'lib/filterinput.php';
include_once 'lib/jmailhelper.php';

$user	=	new userModel();

if(count($arr) > 1)
{
	switch($arr[1])
	{
		case 'check_user':
			$username	=	$_GET['u'];
			$password	=	$_GET['p'];
			die(json_encode(check_login($username, $password)));
			break;
		case 'check_session':
			$urlback	=	$_GET['url'];
			if(isset($_SESSION['user']) && $_SESSION['user'])
			{
				$user	=	json_encode($_SESSION['user']);
				echo "<script>window.opener.location.href='".$urlback.(strpos($urlback, '?') ? '&' : '?')."auth=1&user=$user';window.close();</script>";
			}
			else
			{
				echo "<script>window.close();</script>";
			}
			break;
		case 'register':
			$smarty->assign('base_url',base_url());
			//
			if($_SERVER['REQUEST_METHOD'] == 'POST')
			{
				$username	=	$_POST['username'];
				$password	=	$_POST['password'];
				$fullname	=	$_POST['fullname'];
				$j	=	new JFilterInput();
				$username	=	$j->clean($username);
				$fullname	=	$j->clean($fullname);
				$password	=	$j->clean($password);
				$email		=	$_POST['email'];
				$captcha	=	$_POST['verifycode'];
				$captcha	=	strtoupper($captcha);
				
				
				
				//echo $username;
				$err	=	false;
				$valid	=	array();
				if(!JMailHelper::isEmailAddress($email))
				{
					$err	=	true;
					$valid[]	=	'email1';
				}
				else
				{
					
				}
				
				if($user->check_existed_email($email))
				{
					$err	=	true;
					$valid[]	=	'email2';
				}
				
				if($user->check_existed_username($username))
				{
					$err	=	true;
					$valid[]	=	'username';
				}
				
				if($err == false)
				{
					print_r($valid);
					$data	=	array(
								array('username','password','email','fullname'),
								array($username,md5($password),$email,$fullname)
							);
					
					if($captcha	==	$_SESSION['captcha'])
					{
						if($user_id = $user->insert($data))
						{
							$row	=	$user->get_by_id($user_id);
							$tmp	=	array('username' => $row['username'],'email' => $row['email'],'fullname' => $row['fullname']);
							$_SESSION['user']	=	$tmp;
							$tmp	=	json_encode($tmp);
							setcookie('__bhp_user',$tmp,time()+(60*60*24*30),'/');
						}
						redirect(base_url());
					}
					
				}
			}
			break;
			//end register
		case 'login':
			break;//end login
		case 'logout':
			if(isset($_SESSION['user'])){
				unset($_SESSION['user']);
			}
			if(isset($_GET['url'])){
				$url	=	$_GET['url'];
				echo "<script>window.parent.location.href='$url';</script>";
				exit();
			}
			else
			{
				redirect(base_url());
			}
			
			break;//end loguot
	}
}

$file	=	TMPL_DIR.$module->get_class().'/'.$module->get_function().'.tmpl';

if(file_exists($file))
	$smarty->display($file);

?>
<?php include_once 'layout/footer.php';?>